package com.buding.fx.service.impl;

import com.buding.fx.anotation.SearchInfo;
import com.buding.fx.anotation.Show;
import com.buding.fx.common.constant.CssStyleInfo;
import com.buding.fx.entity.Menu;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;


/**
 * @author mingdongwei-jwk
 * @date 2022/09/30 14:39
 * @class com.buding.fx.service.impl
 * @Description
 */
public class BasePageBuildSerivce {
    Logger logger = LoggerFactory.getLogger(BasePageBuildSerivce.class);

    /**
     * 构建顶部导航栏具体实现
     * @param css
     * @param title
     * @return
     */
    Node addTitle(CssStyleInfo css, String title){
        HBox hBox = new HBox();
        hBox.setPrefWidth(css.getCenter_weight());
        hBox.setPrefHeight(css.getCenter_title_height());
        Border border = new Border(new BorderStroke(Color.web(css.getLeft_node_right_border_color()), BorderStrokeStyle.SOLID,null,new BorderWidths(0,0,1,0)));
        hBox.setBorder(border);
        hBox.setSpacing(5);
        hBox.setAlignment(Pos.CENTER_LEFT);
        ImageView imageView = new ImageView(new Image("/img/icon/square-small.png"));
        imageView.setFitWidth(15);
        imageView.setFitHeight(15);
        Label label = new Label(title);
        label.setPrefWidth(css.getCenter_weight());
        label.setStyle("-fx-font-size: 13; -fx-text-fill: #484948");
        label.setTextFill(Color.rgb(90,90,90));
        hBox.getChildren().addAll(imageView,label);
        return hBox;
    }

    /**
     * 根据menu的默认compmant类名生成默认的搜索框区域,默认搜索框根据menu的处理类名实体类的注解构建（为了查询和展示方便，使用一个类作为处理器结果集合）
     * @param menu
     * @param css
     * @return
     */
    Node  addSearchNode(Menu menu, CssStyleInfo css){
        FlowPane flowPane = new FlowPane();
        flowPane.setPrefWidth(css.getCentr_serch_width());
        BackgroundFill backgroundFill = new BackgroundFill(Color.web(css.getCenter_search_background_color()),new CornerRadii(0),new Insets(0,0,0,0));
        Background background = new Background(backgroundFill);
        flowPane.setBackground(background);
        fastBuildDefaltNode(flowPane,menu.getComponent(),css);
        flowPane.setHgap(20);
        flowPane.setVgap(10);
        flowPane.setPadding(new Insets(5,0,5,10));
        flowPane.setAlignment(Pos.CENTER_LEFT);
        return flowPane;
    }


    /**
     * 根据类名各种注解实现默认的构造搜索框
     * @param className
     * @param css
     */
    private void fastBuildDefaltNode(FlowPane flowPane,String className,CssStyleInfo css){
        try{
            if (StringUtils.isNotEmpty(className)){
                Class<?> aClass = Class.forName(className);
                Object object= aClass.newInstance();
                if (object!=null){
                     Show showAnotation = aClass.getAnnotation(Show.class);
                     if (showAnotation!=null){
                         boolean show = showAnotation.isShow();
                         if (show){
                             Field[] fields =aClass.getDeclaredFields();
                             for (Field field:fields){
                                 SearchInfo searchInfoAnotation = field.getAnnotation(SearchInfo.class);
                                 if (searchInfoAnotation!=null){
                                     String desc = searchInfoAnotation.desc();
                                     double width = searchInfoAnotation.width();
                                     double height = searchInfoAnotation.height();
                                     int fontSize = searchInfoAnotation.fontsize();
                                     String familyName = searchInfoAnotation.familyName();
                                     String fontColor = searchInfoAnotation.fontColor();
                                     Label label = new Label(desc+": ");
                                     FontWeight fontWeight = FontWeight.EXTRA_BOLD;
                                     FontPosture fontPosture = FontPosture.ITALIC;
                                     Font tfont = Font.font(familyName, fontWeight , fontPosture, fontSize);
                                     label.setFont(tfont);
                                     label.setTextFill(Color.web(fontColor));
                                     GridPane gridPane = new GridPane();
                                     TextField textField = new TextField();
                                     textField.setPrefWidth(width);
                                     textField.setPrefHeight(height);
                                     gridPane.add(label,0,0);
                                     gridPane.add(textField,1,0);
                                     flowPane.getChildren().add(gridPane);

                                 }
                             }
                         }
                     }

                }
            }
        }catch (Exception e){
            logger.error("parse class erro: {} ",e);
        }
    }
}
